/*
 *  btree.h
 *  rr-module-text
 *
 *  Created by Rodrigo Sestari on 02/04/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#ifndef _BTREE_H
#define _BTREE_H
#define _BTREE_VERSION 0.01

struct btree
/* This is the struture that is returned by btree_init() and
 *    must be supplied to every function as it contains all
 *    the data for one specific AVL tree.
 */
{
    struct AVLnode *head;
    int size;
    void (*destroy_data)(void *);
    void (*destroy_key)(void *);
    int (*compare)(void *, void *);
};

struct btree *btree_create(void (*destroy_data)(void *),
                           void (*destroy_key)(void *),
                           int (*compare)(void *, void *));
/*
 * Creates and initilizes a struct btree.  This function must
 *    be called before any other function can be used.  destroy_data
 *    is a pointer to the function that deallocates the data.
 *    destroy_key is a pointer to a function that deallocates
 *    the key.  compare is a pointer to a function that compares
 *    two keys.  If the first key is greater than the second an
 *    int greater than 0 must be returned, 0 for equal <0 other
 *    wise.
 */

void btree_destroy(struct btree *tree);
/*
 * Deallocates the memory used by the tree and the struct btree.  This
 *    function uses the deallocation functions supplied to btree_create().
 */

int btree_insert(struct btree *tree, void *key, void *data);
/*
 * Inserts a node into the tree.
 *    tree - the tree to insert into
 *    key - void pointer to key data
 *    data - void pointer to data
 *    returns 0 on success
 */

int btree_remove(struct btree *tree, void *key);
/*
 * Removes (actually hides) a node from the tree.
 *    tree - the tree to remove from
 *    key - the key of the node to remove.
 *    returns 0 on success.
 */

void *btree_lookup(struct btree *tree, void *key);
/*
 * returns the pointer to data specified by key
 *    tree - tree to search
 *    key - key to find
 *    returns NULL if key is not found
 */

int btree_size(struct btree *tree);
/*
 * Retunrs the number of nodes in tree
 *    tree - the tree to count
 */

void btree_dump(struct btree *tree, void *topass,
                void (*print)(void *key, void *data, void *topass));
/*
 * Performs an in order dump of the tree.
 *    tree - tree to dump
 *    topass - pointer to data to pass to "print" function
 *    print(void *key, void *data) - Function to handle key
 *        and data.
 */

#endif